<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\IndexController;
use App\Models\SystemManagement\Organization;
use App\Models\SystemManagement\OrganizationEmployee;
use Illuminate\Http\Request;

/**
 * Class OrganizationController
 * @package App\Http\Controllers\SystemManagement
 * @group 组织管理
 */
class OrganizationController extends IndexController
{
    /**
     * 添加组织
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 组织名称
     * @bodyParam parent_id int 组织父ID
     * @bodyParam address string required 组织地址
     * @bodyParam description string required 描述信息
     * @bodyParam logo int required 组织logo id
     *
     * @response 201 {"code":201,"msg":"created","data":"成功"}
     */
    public function createOrganization(\App\Http\Requests\System\Organization $request){
        return $this->create($request,new Organization());
    }

    /**
     * 组织列表--分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam name string 组织名称
     * @queryParam parent_id int 组织父ID
     * @queryParam address string 组织地址
     * @queryParam description string 描述信息
     * @queryParam file->local_url string logo url地址
     *
     * @response 200 {"code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"Not Found","data":"\u6ca1\u6709\u67e5\u8be2\u5230\u4efb\u4f55\u6570\u636e"}
     */
    public function getOrganization(){
        $per = \request()->input('per_page');
        return $this->getList(new Organization(),$per);
    }

    /**
     * 查看组织
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required 组织id
     *
     * @queryParam name string 组织名称
     * @queryParam parent_id int 组织父ID
     * @queryParam address string 组织地址
     * @queryParam description string 描述信息
     * @queryParam file->local_url string logo url地址
     *
     * @response 200 {"code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"Not Found","data":"\u6ca1\u6709\u67e5\u8be2\u5230\u4efb\u4f55\u6570\u636e"}
     */
    public function getOneOrganization($id){
        return $this->show($id,new Organization());
    }

    /**
     * 修改组织
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required 组织id
     * @bodyParam name string required 组织名称
     * @bodyParam parent_id int 组织父ID
     * @bodyParam address string required 组织地址
     * @bodyParam description string required 描述信息
     * @bodyParam logo int required 组织logo id
     *
     * @queryParam name string 组织名称
     * @queryParam parent_id int 组织父ID
     * @queryParam address string 组织地址
     * @queryParam area_id int 组织区域
     * @queryParam description string 描述信息
     * @queryParam logo int required 组织logo id
     *
     * @response 205 {"code":205,"msg":"updated","data":"成功"}
     */
    public function updateOrganization(\App\Http\Requests\System\Organization $request,$id){
        return $this->update($request,$id,new Organization());
    }

    /**
     * 批量删除组织
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array required 日志id
     *
     * @response 204 {"code":204,"msg":"deleted","data":"删除成功"}
     */
    public function del(Request $request){
        return $this->delete($request,new Organization());
    }

    /**
     * 批量恢复组织
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array required 日志id
     *
     * @response 205 {"code":205,"msg":"update","data":"恢复成功"}
     */
    public function rec(Request $request){
        return $this->recover($request,new Organization());
    }

    /**
     * 搜索组织
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 组织名
     *
     * @queryParam name string 组织名称
     * @queryParam parent_id int 组织父ID
     * @queryParam address string 组织地址
     * @queryParam description string 描述信息
     * @queryParam file->local_url string logo url地址
     *
     * @response 200 {"code":200,"msg":"success","data":"成功"}
     * @response 404 {"code":404,"msg":"Not Found","data":"\u6ca1\u6709\u67e5\u8be2\u5230\u4efb\u4f55\u6570\u636e"}
     */
    public function getBy(Request $request){
        return $this->getByCond($request,new Organization());
    }

    /**
     * 获取组织数据 分类排序 (参数二选一)
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int 组织id 获取组织及其下级组织
     * @bodyParam parent_id int 组织父ID 获取组织下的所有组织
     *
     * @queryParam name string 组织名称
     * @queryParam parent_id int 组织父ID
     * @queryParam address string 组织地址
     * @queryParam description string 描述信息
     * @queryParam file->local_url string logo url地址
     *
     * @response 200 {"code":200,"msg":"success","data":"成功"}
     */
    public function getCate(Request $request){
        $data = Organization::getDataToArr();
        $parent_id = $request->input('parent_id',0);
        $id = $request->input('id','');
        $data = generateTree($data,$parent_id);
        $list = [];
        //循环获取组织及其下级组织
        if(is_numeric($id)) {
            foreach ($data as $key => $value) {
                if($value['id'] == $id) {
                    $list[] = $value;
                }
            }
        } else {
            $list[] = $data;
        }
        return response()->json(encodeMsg($list,200));
    }

    /**
     * 设置组织负责人
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam organization_id 组织ID
     * @queryParam employee_id 员工id字符串1,2,3
     *
     * @response {"code":200,"msg":"success","data":"组织负责人设置成功"}
     */
    public function setPrincipal(Request $request)
    {
        $organization_id = $request->input('organization_id','');
        if($request->has('employee_id')){
            $ids = OrganizationEmployee::where('organization_id',$organization_id)->get('id')->toArray();//取出中间表中对应部门的id
            $data = [];
            $employee_ids = explode(',',$request->get('employee_id'));
            foreach ($employee_ids as $k=>$value){
                $tmp = [];
                $tmp['employee_id'] = $value;
                $tmp['organization_id'] = $organization_id;
                array_push($data,$tmp);
            }
            try{
                OrganizationEmployee::whereIn('id',$ids)->delete();
                OrganizationEmployee::insert($data);
                return response()->json(encodeMsg(['message' => '组织负责人设置成功!'],200));
            }catch (\Exception $e){
                DB::rollBack();
                return response()->json(encodeMsg(["message" => "组织负责人设置失败，".$e->getMessage()],404), 404);
            }
        }
    }

}
